﻿/*========================== 
 * @author 郑金泉
 * @desc 数组实用工具
 * ========================= */
using System;
using System.Collections.Generic;
using System.Text;

namespace Obsidian.Utils
{

    /// <summary>
    /// 数组相关实用工具
    /// </summary>
    public class ArrayUtil
    {
        /// <summary>
        /// 转化为字符串
        /// </summary>
        /// <param name="arrLong">int64数组</param>
        /// <param name="separator">分割符</param>
        /// <returns></returns>
        public static string Join(long[] arrLong, string separator)
        {
            StringBuilder sb = new StringBuilder();
            if (arrLong.Length > 0)
            {
                foreach (long l in arrLong)
                    sb.Append(l.ToString() + separator);
                sb.Remove(sb.Length - separator.Length, separator.Length);
            }
            return sb.ToString();
        }

        public static string Join(object[] arrObj, string separator)
        {
            StringBuilder sb = new StringBuilder();
            if (arrObj.Length > 0)
            {
                foreach (object obj in arrObj)
                    sb.Append(obj.ToString() + separator);
                sb.Remove(sb.Length - separator.Length, separator.Length);
            }
            return sb.ToString();
        }

        public static string Join(string[] arrStr, string separator)
        {
            StringBuilder sb = new StringBuilder();
            if (arrStr.Length > 0)
            {
                foreach (string str in arrStr)
                    sb.Append(str + separator);
                sb.Remove(sb.Length - separator.Length, separator.Length);
            }
            return sb.ToString();
        }

        public static string Join(IEnumerable<string> strs, string separator)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string str in strs)
                sb.Append(str + separator);
            if (sb.Length > 0)
                sb.Remove(sb.Length - separator.Length, separator.Length);
            return sb.ToString();
        }

        public static string Join<T>(IEnumerable<T> values, string separator)
        {
            StringBuilder sb = new StringBuilder();
            foreach (T v in values)
                sb.Append(Convert.ToString(v) + separator);
            if (sb.Length > 0)
                sb.Remove(sb.Length - separator.Length, separator.Length);
            return sb.ToString();
        }

        /// <summary>
        /// 将字符串转化为int64数组
        /// </summary>
        /// <param name="inputStr">字符串</param>
        /// <param name="separator">分隔符</param>
        /// <returns></returns>
        public static long[] SplitInt64(string inputStr, params char[] separator)
        {
            if (String.IsNullOrEmpty(inputStr))
                return new long[] { };
            string[] arrStr = inputStr.Split(separator);
            long[] arrLong = new long[arrStr.Length];
            for (int i = 0; i < arrStr.Length; i++)
            {
                string str = arrStr[i];
                if (String.IsNullOrEmpty(str))
                    continue;
                arrLong[i] = long.Parse(str);
            }
            return arrLong;
        }

        /// <summary>
        /// 转化为int64数组
        /// </summary>
        /// <param name="objArray"></param>
        /// <returns></returns>
        public static long[] SplitInt64(params object[] objArray)
        {
            long[] arrLong = new long[objArray.Length];
            for (int i = 0; i < objArray.Length; i++)
                arrLong[i] = Convert.ToInt64(objArray[i]);
            return arrLong;
        }

        public static int[] SplitInt32(string inputStr, bool removeEqual, params char[] separator)
        {
            if (!removeEqual)
                return SplitInt32(inputStr, separator);

            Dictionary<string, bool> dict = SplitStringKeysDict(inputStr, separator);
            Dictionary<string, bool>.KeyCollection keys = dict.Keys;
            List<int> list = new List<int>();
            foreach (string str in keys)
            {
                if (String.IsNullOrEmpty(str))
                    continue;
                list.Add(Convert.ToInt32(str));
            }
            return list.ToArray();
        }

        public static int[] SplitInt32(string inputStr, params char[] separator)
        {
            if (String.IsNullOrEmpty(inputStr))
                return new int[] { };
            string[] arrStr = inputStr.Split(separator);
            int[] arrLong = new int[arrStr.Length];
            for (int i = 0; i < arrStr.Length; i++)
            {
                string str = arrStr[i];
                if (String.IsNullOrEmpty(str))
                    continue;
                arrLong[i] = int.Parse(str);
            }
            return arrLong;
        }

        public static bool[] SplitBool(string inputStr, params char[] separator)
        {
            string[] arrStr = inputStr.Split(separator);
            bool[] arrBool = new bool[arrStr.Length];
            for (int i = 0; i < arrStr.Length; i++)
                arrBool[i] = bool.Parse(arrStr[i]);
            return arrBool;
        }

        /// <summary>
        /// 转化为对象数组
        /// </summary>
        /// <param name="arrInt">Int数组</param>
        /// <returns>对象数组</returns>
        public static object[] SplitObject(int[] arrInt)
        {
            object[] arrObj = new object[arrInt.Length];
            for (int i = 0; i < arrInt.Length; i++)
                arrObj[i] = arrInt[i];
            return arrObj;
        }

        public static bool IsIn(string str, string[] arr)
        {
            foreach (string p in arr)
            {
                if (str.Equals(p))
                    return true;
            }
            return false;
        }

        public static bool IsIn(string str, string[] arr, StringComparison comparison)
        {
            foreach (string p in arr)
            {
                if (str.Equals(p, comparison))
                    return true;
            }
            return false;
        }

        private static Dictionary<string, bool> SplitStringKeysDict(string inputStr, params char[] separator)
        {
            string[] arrStr = inputStr.Split(separator);

            Dictionary<string, bool> dict = new Dictionary<string, bool>(arrStr.Length);

            foreach (string str in arrStr)
            {
                if (!dict.ContainsKey(str))
                    dict.Add(str, true);
            }

            return dict;

        }

    }
}
